package com.tcy.game.angryrobots;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.tcy.game.angryrobots.general.CameraHelper;
import com.tcy.game.angryrobots.general.Config;
import com.tcy.game.angryrobots.general.ScoreString;

import static com.tcy.game.angryrobots.Assets.*;

/**
 * StatusView
 * Created by 80002023 on 2016/7/27.
 */
public class StatusView implements ScoreListener, AchievementsListener {

    private static final float ACHIEVEMENT_DISPLAY_TIME = Config.asFloat("status.achievementDisplayTime", 10.0f);
    private static final float ACHIEVEMENT_FLASH_CYCLE = Config.asFloat("status.achievementFlashCycleLength", 1.0f);
    private static final float ACHIEVEMENT_FLASH_ON_PERCENT = Config.asFloat("status.achievementFlashOnPercent", 50.0f);
    private static final float ACHIEVEMENT_FLASH_ON_TIME = ACHIEVEMENT_FLASH_CYCLE * ACHIEVEMENT_FLASH_ON_PERCENT / 100.0f;

    private static final String RESUME_TEXT = "Tap to resume or press <<Back>> to quit";

    private final World world;
    private final OrthographicCamera statusCam;
    private final SpriteBatch spriteBatch;
    private ScoreString scoreString;
    private int lives;
    private StatusManager.Achievements achievement;
    private float achievementTime;
    private float now;

    public StatusView(World world) {
        this.world = world;
        scoreString = new ScoreString();
        statusCam = CameraHelper.createCamera2(CameraHelper.ViewportMode.PIXEL_PERFECT, width, height, Assets.pixelDensity);
        spriteBatch = new SpriteBatch();
        spriteBatch.setProjectionMatrix(statusCam.combined);
    }

    public void render(float delta) {
        now += delta;
        spriteBatch.begin();
        if (!world.isPaused()) {
            drawPauseButton();
            drawScore();
            drawAchievements();
            drawLives();
        } else {
            drawPaused();
        }
        spriteBatch.end();
    }

    private void drawPauseButton() {
        float w = Assets.pauseButton.getRegionWidth() / Assets.pixelDensity;
        float h = Assets.pauseButton.getRegionHeight() / Assets.pixelDensity;
        float y = height - h;
        float x = 0.0f;
        spriteBatch.draw(Assets.pauseButton, x, y, w, h);
    }

    private void drawScore() {
        Assets.scoreFont.setColor(1.0f, 1.0f, 1.0f, 1.0f);
        Assets.scoreFont.draw(spriteBatch, scoreString, 2, height);
        ;
    }

    private void drawAchievements() {
        if (achievement != null) {
            float elapsed = now - achievementTime;
            if (elapsed < ACHIEVEMENT_DISPLAY_TIME) {
                if (elapsed % ACHIEVEMENT_FLASH_CYCLE < ACHIEVEMENT_FLASH_ON_TIME) {
                    String summary = achievement.summary();
                    float x = width * 0.5f;
                    float w = width - x - (Assets.playerWalkingRight1.getRegionHeight() / Assets.pixelDensity) * 4;
                    float y = height - 1 + Assets.textFont.getCapHeight() / 2;
//                    Assets.textFont.drawWrapped(spriteBatch, summary, x, y, w, HAlignment.LEFT);
                    Assets.textFont.draw(spriteBatch, summary, y, w);
                }
            } else {
                achievement = null;
            }
        }
    }

    private void drawLives() {
        float h = Assets.playerWalkingRight1.getRegionHeight() / Assets.pixelDensity;
        float y = height - h;
        float w = Assets.playerWalkingRight1.getRegionWidth() / Assets.pixelDensity;
        float x = width - w;
        for (int i = 0; i < lives - 1; i++) {
            spriteBatch.draw(Assets.playerWalkingRight1, x, y, w, h);
            x -= w;
        }
    }

    private void drawPaused() {
        if (world.getPausedTime() % 1.0f < 0.5f) {
            float y = height - 1 + Assets.scoreFont.getCapHeight() / 2;
            Assets.textFont.setColor(1.0f, 1.0f, 1.0f, 1.0f);
//            Assets.textFont.drawWrapped(spriteBatch, RESUME_TEXT, 0.0f, y, width, HAlignment.CENTER);
            Assets.textFont.draw(spriteBatch, RESUME_TEXT, y, width);
        }
    }

    @Override
    public void onScoreChanged(int score) {
        scoreString.setScore(score);
    }

    @Override
    public void onLivesChanged(int lives) {
        this.lives = lives;
    }

    @Override
    public void onAttained(StatusManager.Achievements achievement) {
        this.achievement = achievement;
        this.achievementTime = now;
    }
}
